//判断用户操作系统
/*var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
var isNexus = u.indexOf('Nexus') > -1; //Nexus 6终端*/

// 创建一个cookie并设置有效时间为 7天
// $.cookie('the_cookie', 'the_value', { expires: 7 });
// 读取cookie
// $.cookie('the_cookie');

var url1 = "http://vote.ukebms.com/common/req";
var url2 = "http://vote.ukebms.com/common/operate";

//请求发送短信
function sendS(t) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "notice.sendCheckCode";
    head.sessionId = "";
    head.userId = "";
    body.telePhone = t;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);

    return response;
}

//手机号登录
function loginU(t, p) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "user.login";
    head.sessionId = "";
    head.userId = "";
    body.telePhone = t;
    var key = "#^vote^#";
    var password = encryptByDES(p, key)
    body.password = password;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);
    sessionStorage.setItem("sessionId", response.response.body.sessionId);
    sessionStorage.setItem("userId", response.response.body.userId);
    sessionStorage.setItem("nickName", response.response.body.nickName);
    sessionStorage.setItem("userAvatar", response.response.body.userAvatar);

    return response;
}

//第三方登陆
function TloginU(t, u, n, ua, g) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "user.thirdPartyLogin";
    head.sessionId = "";
    head.userId = "";
    body.thirdPartyType = t;
    if (t === "1"){
        body.userUuidWX = u;
    }else {
        body.userUuidWB = u;
    }
    body.nickName = n;
    body.userAvatar = ua;
    body.gender = g;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);
    sessionStorage.setItem("sessionId", response.response.body.sessionId);
    sessionStorage.setItem("userId", response.response.body.userId);
    sessionStorage.setItem("nickName", response.response.body.nickName);
    sessionStorage.setItem("userAvatar", response.response.body.userAvatar);

    return response;
}

//手机号唯一验证
function checkTp(t) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "user.checkTelePhone";
    head.sessionId = "";
    head.userId = "";
    body.telePhone = t;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);

    return response;
}

//昵称唯一验证
function checkN(n) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "user.checkNickName";
    head.sessionId = "";
    head.userId = "";
    body.nickName = n;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);

    return response;
}

//注册
function regUser(t, p, c, n) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "user.register";
    head.sessionId = "";
    head.userId = "";
    body.telePhone = t;
    var key = "#^vote^#";
    var password = encryptByDES(p, key)
    body.password = password;
    body.checkCode = c;
    body.nickName = n;

    request.body = body;
    request.head = head;

    rq.request = request;
    var response = r(url1, rq, false);

    return response;
}

//投票图文
function tp(ai, a, aui, ann) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "article.vote";
    head.sessionId = sessionStorage.getItem("sessionId");
    head.userId = sessionStorage.getItem("userId");
    body.articleId = ai;
    body.attitude = a;
    body.articleUserId = aui;
    body.articleNickName = ann;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url2, rq, false);

    return response;
}

//评论图文
function pl(ai, c, a, nn, aui, ann) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "article.comment";
    head.sessionId = sessionStorage.getItem("sessionId");
    head.userId = sessionStorage.getItem("userId");
    body.articleId = ai;
    body.content = c;
    body.attitude = a;
    body.nickName = nn;
    body.articleUserId = aui;
    body.articleNickName = ann;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url2, rq, false);

    return response;
}

//回复评论
function hf(ai, ci, c, cui, nn, aui, ann, rcui, rcnn, a) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    head.type = "article.comment.reply";
    head.sessionId = sessionStorage.getItem("sessionId");
    head.userId = sessionStorage.getItem("userId");
    body.articleId = ai;
    body.commentId = ci;
    body.content = c;
    body.commentUserId = cui;
    body.nickName = nn;
    body.articleUserId = aui;
    body.articleNickName = ann;
    body.replayCommentUserId = rcui;
    body.replayCommentNickName = rcnn;
    body.attitude = a;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url2, rq, false);

    return response;
}

//点赞评论
function dz(ui, ai, aui, ci, cui, t, i) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};

    if (i === 1) {
        head.type = "praise.comment";
    } else {
        head.type = "praise.comment.cancle";
    }
    head.sessionId = sessionStorage.getItem("sessionId");
    head.userId = sessionStorage.getItem("userId");
    body.userId = ui;
    body.articleId = ai;
    body.articleUserId = aui;
    body.commentId = ci;
    body.commentUserId = cui;
    body.type = t;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url2, rq, false);

    return response;
}

//根据参数获取图文内容
function getTw(ai) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};
    var type = "search.articleList.by.parameter";
    head.type = type;
    var sessionId = sessionStorage.getItem("sessionId");
    if (isNull(sessionId) === true) {
        sessionId = "";
    }
    head.sessionId = sessionId;
    var userId = sessionStorage.getItem("userId");
    if (isNull(userId) === true) {
        userId = "";
    }
    head.userId = userId;
    body.articleId = ai;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);

    return response;
}

//用户编号获取用户信息
function getU(u) {
    var rq = {};
    var request = {};
    var head = {};
    var body = {};
    var type = "search.user.byUserId";
    head.type = type;
    head.sessionId = sessionStorage.getItem("sessionId");
    head.userId = sessionStorage.getItem("userId");
    body.searchUserId = u;

    request.body = body;
    request.head = head;

    rq.request = request;

    var response = r(url1, rq, false);

    return response;
}

//发送请求
function r(url, params) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", url, false);
    xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8');
    xhr.send(JSON.stringify(params));
    return JSON.parse(xhr.responseText);
}

//DES 解密 加密
function encryptByDES(message, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    var encrypted = CryptoJS.DES.encrypt(message, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });

    return encrypted.ciphertext.toString();
}

//DES 解密
function decryptByDES(ciphertext, key) {
    var keyHex = CryptoJS.enc.Utf8.parse(key);
    // direct decrypt ciphertext
    var decrypted = CryptoJS.DES.decrypt({
        ciphertext: CryptoJS.enc.Hex.parse(ciphertext)
    }, keyHex, {
        mode: CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return decrypted.toString(CryptoJS.enc.Utf8);
}


//获取url中的字段
function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
};

//计算百分比
function GetPercent(num, total) {
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00 + "%");
}

/**
 * 判断是否null
 * @param data
 */
function isNull(data) {
    return (data === "" || data === undefined || data == null) ? true : data;
}

/**
 * 判断是否数组
 * @param data
 */
var isArray = function (data) {
    return Object.prototype.toString.call(data) === '[object Array]';
}

/**
 * 判断是否登陆
 */
function isLogin() {
    var sessionId = sessionStorage.getItem("sessionId");
    var isn = isNull(sessionId);
    if (isn === true) {
        /*location.href = "login.html";*/
        return true;
    }
}

/**
 * 投票
 * @param t
 */
function toup(t) {

    isLogin();

    var a = sessionStorage.getItem("autitude");
    if (isNull(a) === true || a === "null") {
        var articleNickName = sessionStorage.getItem("articleNickName");
        var articleUserId = sessionStorage.getItem("articleUserId");
        var articleId = sessionStorage.getItem("articleId");
        var attitude = null;
        if (t === 1) {
            attitude = "L";
        } else {
            attitude = "R";
        }
        var t = tp(articleId, attitude, articleUserId, articleNickName);
        sessionStorage.setItem("autitude", attitude);
        firstLoginGetDate();
    }
}

/**
 * 投票评论
 */
function pltw() {

    isLogin();

    var t = $("#t").val();

    var ai = sessionStorage.getItem("articleId");
    var content = $("#plContent").html();
    var nn = sessionStorage.getItem("nickName");
    var rcui = $("#ui").val();
    var aui = sessionStorage.getItem("articleUserId");
    var ann = sessionStorage.getItem("articleNickName");
    var a = sessionStorage.getItem("attitude")
    if (isNull(a) === true) {
        a = "";
    }
    var ci = $("#d").val();
    var rcnn = $("#nn").val();
    var r = "";
    if (t === "0") {
        r = pl(ai, content, a, nn, aui, ann);
    } else if (t === "2") {
        r = hf(ai, ci, content, "", nn, aui, ann, rcui, rcnn, a);
    } else {
        var cii = $("#ci").val();
        var cui = sessionStorage.getItem("userId");
        r = hf(ai, cii, content, cui, nn, aui, ann, rcui, rcnn, a);
    }

    if (r.response.head.status === "0") {
        $("#plContent").hide();
        location.reload();
    }
}

/**
 * 点赞评论
 */
function dzpl(x) {

    isLogin();

    var name = $(x).attr("name");
    var ui = sessionStorage.getItem("userId");
    var ai = sessionStorage.getItem("articleId");
    var aui = sessionStorage.getItem("articleUserId");
    var ci = $(x).attr("d");
    var cui = $(x).attr("ui");
    var t = $(x).attr("t");
    if (name === "0") {
        var r = dz(ui, ai, aui, ci, cui, t, 1);
        if (r.response.head.status === "0") {
            $(x).attr("name", "1");
            $(x).attr("src", "image/图文详情－评论去－点赞icon－默认@3x.png");
        }
    } else {
        var r = dz(ui, ai, aui, ci, cui, t, 2);
        if (r.response.head.status === "0") {
            $(x).attr("name", "0");
            $(x).attr("src", "image/图文详情－评论去－点赞icon－点击@3x.png");
        }
    }
}

/**
 * 登陆请求数据
 */
function firstLoginGetDate() {
    var a = localStorage.getItem("articleId");
    var tw = getTw(a);
    sessionStorage.setItem("articleNickName", tw.response.body.list[0].articleNickName);
    sessionStorage.setItem("articleContent", tw.response.body.list[0].articleContent);
    sessionStorage.setItem("articleUserId", tw.response.body.list[0].articleUserId);
    sessionStorage.setItem("articleId", tw.response.body.list[0].articleId);
    var aUserImg = baseUrl + tw.response.body.list[0].userAvatar;
    if (isNull(tw.response.body.list[0].userAvatar) === true) {
        aUserImg = "image/head.png";
    }
    $("#headImage").attr("src", aUserImg);
    $("#articleNickName").html(tw.response.body.list[0].articleNickName);
    $("#articleCreateTime").html(tw.response.body.list[0].createTime);
    $("#articleContent").html(tw.response.body.list[0].articleContent);
    $("#divImage").attr("src", baseUrl + tw.response.body.list[0].resourceUrl);
    $("#leftExpression").attr("src", baseUrl1 + tw.response.body.list[0].attitudeL);
    $("#rightExpression").attr("src", baseUrl1 + tw.response.body.list[0].attitudeR);

    var left = tw.response.body.list[0].attitudeLeft;
    var right = tw.response.body.list[0].attitudeRight;
    var counrNum = left + right;
    var leftPercent = GetPercent(left, counrNum);
    var rightPercent = GetPercent(right, counrNum);
    $("#leftPercent").html(leftPercent);
    $("#rightPercent").html(rightPercent);
    var num = 8.4375;
    var leftH = num * (left / counrNum);
    var rightH = num * (right / counrNum);
    $("#leftH").css("height", leftH + "rem");
    $("#rightH").css("height", rightH + "rem");

    $("#canYu").html(counrNum);
    $("#pingLun").html(tw.response.body.list[0].commentCount);

    var autitude = tw.response.body.list[0].autitude;
    sessionStorage.setItem("autitude", autitude);
    if (isNull(autitude) !== true) {
        if (autitude === "L") {
            $("#touMing").show();
            $("#imageLeft").css("visibility", "visible");
            $("#leftPercent").css("visibility", "visible");
            $("#leftH").css("visibility", "visible");
            $("#imageRight").css("visibility", "hidden");
            $("#rightPercent").css("visibility", "visible");
            $("#rightH").css("visibility", "visible");
        } else {
            $("#touMing").show();
            $("#imageLeft").css("visibility", "hidden");
            $("#leftPercent").css("visibility", "visible");
            $("#leftH").css("visibility", "visible");
            $("#imageRight").css("visibility", "visible");
            $("#rightPercent").css("visibility", "visible");
            $("#rightH").css("visibility", "visible");
        }
    }

    var comments = tw.response.body.list[0].comments;
    if (isArray(comments) === true) {
        for (var i = comments.length - 1; i >= 0; i--) {
            var imageUrl = "image/pl.png";

            var headImageUrl = baseUrl + comments[i].userAvatar;
            if (isNull(comments[i].userAvatar) === true) {
                headImageUrl = "image/head.png";
            }
            if (isNull(comments[i].replayCommentUserId) === true) {
                $("#comment").append('' +
                    '<div class="commentDiv">' +
                    '<div class="commentDivC1">' +
                    '<div class="commentW commentH commentHeadImage">' +
                    '<img class="commentW commentH" src="' + headImageUrl + '">' +
                    '</div>' +
                    '<div class="commentH commentNickName">' +
                    '<div style="color: black;font-size: 0.75rem;margin-top: 0.09375rem;">' + comments[i].nickName + '</div>' +
                    '<div style="color: #98a0a9;font-size: 0.625rem;margin-top:0.01rem;">' + comments[i].savetime + '</div>' +
                    '</div>' +
                    '<div class="commentH commentDz">' +
                    '<button type="button" d="' + comments[i].id + '" ci="' + comments[i].commentId + '" ui="' + comments[i].userId + '" t="' + comments[i].type + '" nn="' + comments[i].nickName + '" onclick="showPl(this);" name="' + comments[i].praiseSize + '" src="' + imageUrl + '" style="width:2.5rem;cursor:pointer;color: #808080;float: right;border: 1px #e6e6e6 solid;background-color: #FFFFFF;outline:none;letter-spacing: 0.05rem;font-size: 0.7rem;">回复</button>' +
                    '</div>' +
                    '</div>' +
                    '<div class="commentDivC2">' + comments[i].content + '</div>' +
                    '</div>');
            } else {
                $("#comment").append('' +
                    '<div class="commentDiv">' +
                    '<div class="commentDivC1">' +
                    '<div class="commentW commentH commentHeadImage">' +
                    '<img class="commentW commentH" src="' + headImageUrl + '">' +
                    '</div>' +
                    '<div class="commentH commentNickName">' +
                    '<div style="color: black;font-size: 0.75rem;margin-top: 0.09375rem;">' + comments[i].nickName + '</div>' +
                    '<div style="color: #98a0a9;font-size: 0.625rem;margin-top:0.01rem;">' + comments[i].savetime + '</div>' +
                    '</div>' +
                    '<div class="commentH commentDz">' +
                    '<button type="button" d="' + comments[i].id + '" ci="' + comments[i].commentId + '" ui="' + comments[i].userId + '" t="' + comments[i].type + '" nn="' + comments[i].nickName + '" onclick="showPl(this);" name="' + comments[i].praiseSize + '" src="' + imageUrl + '" style="width:2.5rem;cursor:pointer;color: #808080;float: right;border: 1px #e6e6e6 solid;background-color: #FFFFFF;outline:none;letter-spacing: 0.05rem;font-size: 0.7rem;">回复</button>' +
                    '</div>' +
                    '</div>' +
                    '<div class="commentDivC2">回复 ' + comments[i].replayCommentNickName + '：' + comments[i].content + '</div>' +
                    '</div>');
            }
        }
    }

}
